package com.tp7567.web.common;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.AjaxLink;
import org.apache.wicket.ajax.markup.html.form.AjaxSubmitLink;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.link.AbstractLink;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.ResourceModel;

@SuppressWarnings("serial")
public abstract class ButtonPanel extends Panel {
	
	private Boolean submit;
	
	public ButtonPanel(String id, Boolean submit) {
		super(id);
		this.submit = submit;
		
		AbstractLink ajaxLink = createLink("link");
		add(ajaxLink);
		ajaxLink.add(new Label("buttonText", new ResourceModel(id)));
	}
	
	protected AbstractLink createLink(String linkId) {
		if (submit) {
			return new AjaxSubmitLink(linkId) {
				
				@Override
				protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
					ButtonPanel.this.onClick(target);
				}
			};
		} else {
			return new AjaxLink<Void>(linkId) {
				
				@Override
				public void onClick(AjaxRequestTarget target) {
					ButtonPanel.this.onClick(target);
				}
			};
		}
	}
	
	protected abstract void onClick(AjaxRequestTarget target);
	
}
